import Cookies from 'js-cookie'
import {getUserInfo} from "@/api/personalUser";

const TokenKey = 'Xu-Blog-Token';
const UserInfoKey = 'Xu-Blog-User';

export function getToken() {
  return Cookies.get(TokenKey)
}

export function getCookieUser(){
  if(Cookies.get(UserInfoKey)){
    return JSON.parse(Cookies.get(UserInfoKey));
  }else {
    return undefined;
  }
}

export async function  setToken(token) {
  await Cookies.set(TokenKey, token);
  // 每次设置token的时候 获取一下用户信息
  let res = await getUserInfo();
  await Cookies.set(UserInfoKey,JSON.stringify(res.data));
}

export function removeToken() {
  // 每次移除token的时候 同样移除用户信息
  Cookies.remove(UserInfoKey);
  return Cookies.remove(TokenKey)
}
